/**
 * 
 */
package kr.pe.silent.etweeter.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * @author Sanghoon Lee
 * $Id: IntroPanel.java 15 2010-11-15 08:40:42Z calm1979 $
 */
public class IntroPanel {

	private Composite mComposite;

	private Text text;
	private Button button;


	private static final String INTRO_MESSAGE = "1. 브라우저가 열리면 트위터에 로그인\n" +
			"2. 로그인 후 PIN 아이디를 아래에 입력";
	
	public IntroPanel(Composite parent) {
		createPanel(parent);
	}

	private void createPanel(Composite parent) {
		mComposite = new Composite(parent, SWT.NONE);
		mComposite.setLayout(new GridLayout(2, false));
		
		GridData gd;
		
		Label label = new Label(mComposite, SWT.WRAP);
		label.setText(INTRO_MESSAGE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		label.setLayoutData(gd);
		
		text = new Text(mComposite, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.keyCode == SWT.CR || e.keyCode == SWT.LF) {
					button.notifyListeners(SWT.Selection, new Event());
				}
			}
		});
		
		button = new Button(mComposite, SWT.PUSH);
		button.setText("Add");
		gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		button.setLayoutData(gd);
	}
	
	public Control getControl() {
		return mComposite;
	}
	
	public String getPin() {
		return text.getText().trim();
	}
	
	public void addSelectionListener(SelectionListener listener) {
		button.addSelectionListener(listener);
	}
	
	public void dispose() {
		Composite parent = mComposite.getParent();
		mComposite.dispose();
		parent.layout();
	}
	
}
